package com.zhkj.crawl.rank.ext;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChromeExt {
    private static final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");

    public static void main(String[] args) {
        //driver.get("https://www.ip.cn/api/index?ip=&type=0");
//        driver.get("https://github.com/sxei/chrome-plugin-demo");
//        driver.get("https://bot.sannysoft.com/");
        ChromeDriver driver = initDriver();

        go(driver, "https://www.amazon.co.uk/dp/B00HA2INXC?language=en_US");
        go(driver, "https://www.amazon.com/dp/B0761J2S53?language=en_US");
    }

    private static Document go(ChromeDriver driver, String url){
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebElement webElement = driver.findElement(By.xpath("/html"));
        String responseContent = webElement.getAttribute("outerHTML");
        return Jsoup.parse(responseContent);
    }

    private static ChromeDriver initDriver() {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setLogLevel(ChromeDriverLogLevel.OFF);
        if(!isWindows){
            chromeOptions.addArguments("-headless");
        }
        chromeOptions.addArguments("--silent");
        chromeOptions.addArguments("log-level=3");
        // chromeOptions.addExtensions(new File("D:\\data\\logistics-group\\crawl-service\\src\\main\\resources\\proxyExtension\\proxyExtension.zip"));
//        chromeOptions.addArguments("load-extension=D:\\data\\logistics-group\\crawl-service\\src\\main\\resources\\AutoPort");
//        chromeOptions.addArguments("load-extension=D:\\data\\logistics-group\\crawl-service\\src\\main\\resources\\blankPage");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));


        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("source", " Object.defineProperty(navigator, 'webdriver', {\n" +
                "      get: () => undefined\n" +
                "    })");
        chromeDriver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", parameters);

        return chromeDriver;
    }

    static {
        Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");

        if (isWindows) {
            System.setProperty("webdriver.chrome.driver", "D:\\Program Files\\chromedriver_91_0_4472_101.exe");
        }
    }
}
